﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using com.dz.ct.common;
using MySql.Data.MySqlClient;

namespace com.dz.ct.db
{
    public class BookDao
    {
        /// <summary>
        /// 添加书的信息
        /// </summary>
        public BookInfo InsertBookInfo(BookInfo i_book_info)
        {
            try
            {
                

                string t_str_sql = string.Format("select  bookId,bookName,bookChapterListUrl,lastUpdateChapterId,lastUpdateChapterName from tbBook where bookName='{0}' and siteId={1}", i_book_info.BookName, i_book_info.SiteId);

                BookInfo t_book_info = null;

                using (MySqlDataReader t_reader = DbHelperMySQL.ExecuteReader(t_str_sql))
                {

                    while (t_reader.Read())
                    {
                        t_book_info = new BookInfo();
                        t_book_info.BookId = t_reader["bookId"].ToString();
                        t_book_info.BookName = t_reader["bookName"].ToString();
                        t_book_info.BookChapterListUrl = t_reader["bookChapterListUrl"].ToString();
                        t_book_info.LastUpdateChapterId = t_reader["lastUpdateChapterId"].ToString();
                        t_book_info.LastUpdateChapterName = t_reader["lastUpdateChapterName"].ToString();
                    }
                }

                if(t_book_info!=null)
                    return t_book_info;
               
                t_str_sql=string.Format("insert into tbbook(bookId,bookName,bookAuthorName,bookDescription,bookCategoryName,bookSubCategoryName,bookCollect,bookClick,bookUrl,bookChapterListUrl,bookImageUrl,bookCommentUrl,bookDownLoadUrl,isVip,siteId)values('{0}','{1}','{2}','{3}','{4}','{5}',{6},{7},'{8}','{9}','{10}','{11}','{12}',{13},{14})",i_book_info.BookId,i_book_info.BookName,i_book_info.BookAuthorName,i_book_info.BookDescription,i_book_info.BookCategoryName,i_book_info.BookSubCategroyName,i_book_info.BookCollect,i_book_info.BookClick,i_book_info.BookUrl,i_book_info.BookChapterListUrl,i_book_info.BookImageUrl,i_book_info.BookCommentUrl,i_book_info.BookDownLoadUrl,i_book_info.IsVip==true?1:0,i_book_info.SiteId);

                DbHelperMySQL.ExecuteSql(t_str_sql);

                return i_book_info;
  
            }
            catch (Exception ex)
            {
                throw new DataException("添加书信息出现异常", ex);
            }
        }

        /// <summary>
        /// 检查小说是否存在
        /// </summary>
        /// <param name="i_book_name"></param>
        /// <param name="i_site_id"></param>
        /// <returns></returns>
        public BookInfo CheckBookEixsts(string i_book_name, int i_site_id)
        {
            try
            {
                string t_str_sql = string.Format("select bookId,bookName,bookChapterListUrl,lastUpdateChapterId,lastUpdateChapterName from tbBook where bookName='{0}' and siteId={1}", i_book_name, i_site_id);
                BookInfo t_book_info = null;
                using (MySqlDataReader t_reader = DbHelperMySQL.ExecuteReader(t_str_sql))
                {
                    while (t_reader.Read())
                    {
                        t_book_info = new BookInfo();
                        t_book_info.BookId = t_reader["bookId"].ToString();
                        t_book_info.BookName = t_reader["bookName"].ToString();
                        t_book_info.BookChapterListUrl = t_reader["bookChapterListUrl"].ToString();
                        t_book_info.LastUpdateChapterId = t_reader["lastUpdateChapterId"].ToString();
                        t_book_info.LastUpdateChapterName = t_reader["lastUpdateChapterName"].ToString();
                    }

                }

                return t_book_info;
            }
            catch (Exception ex)
            {
                throw new DataException("检查小说是否存在失败", ex);
            }
        }

        /// <summary>
        /// 添加章节信息
        /// </summary>
        /// <param name="i_volume_name"></param>
        /// <param name="i_chapter_name"></param>
        /// <param name="i_update_time"></param>
        /// <param name="i_is_vip"></param>
        /// <param name="i_words_count"></param>
        /// <param name="i_book_id"></param>
        /// <param name="i_site_id"></param>
        /// <returns></returns>
        public int InsertBookChapterInfo(BookChapterInfo i_book_chapter_info)
        {
            string t_str_sql = string.Format("INSERT INTO [tb{9}]([bookId], [chapterId], [chapterName],  [isVip], [chapterUrl], [preChapterId], [nextChapterId], [updateTime], [siteId])VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')",
                i_book_chapter_info.BookId, i_book_chapter_info.ChapterId, i_book_chapter_info.ChapterName,  i_book_chapter_info.IsVip?1:0, i_book_chapter_info.ChapterUrl, i_book_chapter_info.PreChapterId, i_book_chapter_info.NextChapterId, i_book_chapter_info.UpdateTime, i_book_chapter_info.SiteId,  Consts.GetTableName(i_book_chapter_info.SiteId));
            try
            {
               // int t_ret = DbHelperSQL.ExecuteSql(t_str_sql);
               // return t_ret;
                return DbHelperMySQL.ExecuteSql(t_str_sql);
            }
            catch (Exception ex)
            {
                throw new DataException("添加章节信息失败", ex);
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="i_str_sql"></param>
        /// <returns></returns>
        public DataSet QueryData(string i_str_sql)
        {
            try
            {
                //return DbHelperSQL.Query(i_str_sql);
                return DbHelperMySQL.Query(i_str_sql);
            }
            catch (Exception ex)
            {
                throw new DataException("查询数据失败", ex);
            }
        }

        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="i_str_sql"></param>
        /// <returns></returns>
        public int ExcuteSql(string i_str_sql)
        {
            try
            {
                //return DbHelperSQL.ExecuteSql(i_str_sql);
                return DbHelperMySQL.ExecuteSql(i_str_sql);
            }
            catch (Exception ex)
            {
                throw new DataException("执行sql语句失败", ex);
            }
        }
       
    }
}
